package com.atguigu.mapreduce.solution;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

public class SolutionMapper2 extends Mapper<Text, Text, Text, Text> {
    private final Text key = new Text();

    @Override
    protected void map(Text key, Text value, Mapper<Text, Text, Text, Text>.Context context) throws IOException, InterruptedException {
        String[] friends = value.toString().split(",");

        // key为所有value的共同好友
        for (int i = 0; i < friends.length; i++) {
            for (int j = i + 1; j < friends.length; j++) {
                if (friends[i].compareTo(friends[j]) <= 0) {
                    this.key.set(friends[i] + "-" + friends[j]);
                } else {
                    this.key.set(friends[j] + "-" + friends[i]);
                }
                context.write(this.key, key);
            }
        }

    }
}
